           package com.java.diagnostics.visualizer.impl.logging;
           
           import java.io.PrintWriter;
           import java.io.StringWriter;
           import java.util.logging.Formatter;
           import java.util.logging.LogRecord;
           
           public class TraceFormatter extends Formatter
           {
             private static final String FOUND_EXCEPTION = "Found exception: ";
             private static final String NEWLINE = System.getProperty("line.separator");
             private static long INITIALIZATION_MILLIS = System.currentTimeMillis();
             private static long LAST_MILLIS = INITIALIZATION_MILLIS;
           
             public String format(LogRecord record) {
               String loggerName = record.getLoggerName();
               if (loggerName.indexOf('.') > -1) {
                 loggerName = loggerName.substring(loggerName.lastIndexOf('.') + 1);
               }
           
               StringBuffer buffer = new StringBuffer();
               buffer.append(
                 String.format("%4d %5d [%4d] %s %s", new Object[] { 
                 Long.valueOf(record.getMillis() - INITIALIZATION_MILLIS), 
                 Long.valueOf(record.getMillis() - LAST_MILLIS), 
                 Integer.valueOf(record.getThreadID()), 
                 loggerName, 
                 record.getMessage() }));
           
               buffer.append(NEWLINE);
           
               Throwable ex = record.getThrown();
               while (ex != null) {
                 buffer.append("Found exception: ");
                 buffer.append(ex.getMessage());
                 buffer.append(NEWLINE);
                 buffer.append(getStackTraceAsString(ex));
                 ex = ex.getCause();
               }
               LAST_MILLIS = record.getMillis();
               return buffer.toString();
             }
           
             private String getStackTraceAsString(Throwable exception)
             {
               StringWriter sw = new StringWriter();
               exception.printStackTrace(new PrintWriter(sw, true));
               return sw.toString();
             }
           }

